#pragma once
#include <vector>
namespace little_monster
{
	template <class T>
	struct less
	{
		bool operator()(const T& left, const T& right)
		{
			return left < right;
		}
	};
	template <class T>
	struct greater
	{
		bool operator()(const T& left, const T& right)
		{
			return left > right;
		}
	};

	template<class T, class Container = std::vector<T>, class Compare = less<T>>
	class priority_queue
	{
	public:

		priority_queue()
			:_c()
		{}

		template <class Iterator>
		priority_queue(Iterator first, Iterator last)
			: _c(first, last)
		{
			int count = _c.size();
			int root = ((count - 2) >> 1);
			for (; root >= 0; root--)
			{
				AdjustDown(root);
			}
		}
		void push(const T& x)
		{
			_c.push_back(x);
			AdjustUp(_c.size() - 1);
		}

		void pop()
		{
			if (empty())
				return;
			std::swap(_c.front(), _c.back());
			_c.pop_back();
			AdjustDown(0);
		}

		size_t size() const
		{
			return _c.size();
		}

		bool empty() const
		{
			return _c.empty();
		}

		const T& top() const
		{
			return _c.front();
		}

	private:
		void AdjustDown(int parent)
		{
			size_t child = 2 * parent + 1;
			while (child < _c.size())
			{
				if (child + 1 < _c.size() && Compare()(_c[child], _c[child + 1]))
				{
					++child;
				}
				if (Compare()(_c[parent], _c[child]))
				{
					std::swap(_c[child], _c[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
					return;
			}
		}

		void AdjustUp(int child)
		{
			size_t parent = ((child - 1) >> 1);
			while (child)
			{
				if (Compare()(_c[parent], _c[child]))
				{
					std::swap(_c[parent], _c[child]);
					child = parent;
					parent = ((child - 1) >> 1);
				}
				else
					return;
			}

		}

	private:
		Container _c;

	};
}